package com.bank.backend.boot.server.service.impl;


import com.bank.backend.boot.base.constant.ExceptionConstant;
import com.bank.backend.boot.base.model.dto.UserDTO;
import com.bank.backend.boot.base.model.entity.User;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.base.model.vo.UserVO;
import com.bank.backend.boot.server.converter.UserConverter;
import com.bank.backend.boot.server.mapper.UserMapper;
import com.bank.backend.boot.server.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author JiaoWei
* @description 针对表【user(用户基本信息)】的数据库操作Service实现
* @createDate 2025-03-29 21:04:26
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private UserConverter userConverter;


    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<UserVO> queryUserList() {
        List<User> users = userMapper.selectList(null);
        return userConverter.convert(users);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserVO queryUserById(Long id) {
        User user = userMapper.selectById(id);
        return userConverter.convert(user);
    }

    /**
     * 分页查询用户信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<UserVO> queryUserPage(int currentPage, int pageSize) {
        Page<User> page = new Page<>(currentPage, pageSize);
        Page<User> userPage = userMapper.selectPage(page, null);
        return userConverter.convert(userPage);
    }

    /**
     * 保存用户信息
     * @param userDTO
     * @return
     */
    @Override
    public Boolean saveUser(UserDTO userDTO) {
        // 查询用户是否已存在
        if (null == userDTO.getIdCard()) {
            throw new GlobalException(ExceptionConstant.ID_CARD_NULL);
        }
        User user = userMapper.queryUserByIdCard(userDTO.getIdCard());
        if (null != user) {
            throw new GlobalException(ExceptionConstant.USER_EXIST);
        }

        User convert = userConverter.convert(userDTO);
        convert.setCreateTime(new Date());
        convert.setUpdateTime(new Date());
        int i = userMapper.insert(convert);
        if (i == 0) {
            return false;
        }
        return true;
    }
}




